﻿using System.Collections;
using System.Data;
using System.Data.SqlClient;
using NHibernate;
using NHibernate.Criterion;
using Spring.Stereotype;
using TSites.Core.DataInterfaces.UserManagerment;
using TSites.Core.Domain.UserMangerment;
using TSites.Core.Repository;
using TSites.Common.Enums;

namespace TSites.Data.UserManagerment
{
	[Repository]
	public class UserDao : NHibernateRepository<User, string>, IUserDao
	{
		public IList GetUserList(string userId, string username, string email, int activeCode,
			int pageIndex, int pageSize, out int totalItems)
		{
			ICriteria crit = CurrentSession.CreateCriteria(typeof(User))
				.Add(Expression.Like("ID", "%" + userId + "%"))
				.Add(Expression.Like("UserName", "%" + username + "%"))
				.Add(Expression.Like("Email", "%" + email + "%"));
			if (activeCode != -1)
			{
			    crit.Add(Expression.Eq("ActiveCode", activeCode));
			}
			// Order by
			crit.AddOrder(Order.Asc("ID"));

			return GetList(crit, pageIndex, pageSize, out totalItems);
		}

		private User GetByUidOrEmail(string uid, string email)
		{
			ICriteria crit = CurrentSession.CreateCriteria(typeof(User))
				.Add(Expression.Or(Expression.Eq("ID", uid), Expression.Eq("Email", email)));

			return GetByCriteria(crit);
		}
		public bool CheckExists(string uid, string email, EXISTS_TYPE type)
		{
			User currentUser = null;
			EXISTS_TYPE existedType = (EXISTS_TYPE)type;
			switch (existedType)
			{
				case EXISTS_TYPE.ALL:
					currentUser = GetByUidOrEmail(uid, email);
					break;

				case EXISTS_TYPE.USER_ID:
					currentUser = GetByID(uid);
					break;

				case EXISTS_TYPE.EMAIL:
					currentUser = GetByEmail(email);
					break;

				default:
					break;
			}
			return currentUser == null ? false : true;
		}

		public bool CheckEmailExists(string email, string uid)
		{
			ICriteria crit = CurrentSession.CreateCriteria(typeof(User))
				.Add(!Expression.IdEq(uid))
				.Add(Expression.Eq("Email", email));
				
			User currentUser = GetByCriteria(crit);
			return currentUser == null ? false : true;
		}

		public int VerifyActivedKey(string key, int activedCode)
		{
			IDbCommand command = new SqlCommand();
			command.Connection = CurrentSession.Connection;
			command.CommandType = CommandType.StoredProcedure;
			command.CommandText = "SP_UPD_USER_MGMT_KEYVERIFY";

			// Set input parameter, 
			command.Parameters.Add(new SqlParameter("@key", key));
			command.Parameters.Add(new SqlParameter("@activeCode", activedCode));

			// Set output parameter
			SqlParameter outputParameter =
				new SqlParameter("@returnVal", SqlDbType.Int)
				{
					Direction = ParameterDirection.Output
				};
			command.Parameters.Add(outputParameter);

			// Executing the stored procedure.
			command.ExecuteNonQuery();

			// Getting the value set by the stored procedure in the output parameter.
			object returnValue = outputParameter.Value;
			if (returnValue != null)
			{
				return (int)returnValue;
			}
			return 0;
		}

		public User GetByEmail(string email)
		{
			ICriteria crit = CurrentSession.CreateCriteria(typeof(User))
				.Add(Expression.Eq("Email", email));
			return GetByCriteria(crit);
		}

		public User GetByKeyVerify(string keyVerify)
		{
			ICriteria crit = CurrentSession.CreateCriteria(typeof(User))
				.Add(Expression.Eq("KeyVerify", keyVerify));
			return GetByCriteria(crit);
		}
	}
}
